package com.example.demo.config.encrypt.handler;//package com.example.demo.config.handler;
//
//import com.quartz.demo.annotation.SensitiveData;
//import com.quartz.demo.service.EncryptService;
//import org.apache.ibatis.executor.parameter.ParameterHandler;
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.plugin.Intercepts;
//import org.apache.ibatis.plugin.Invocation;
//import org.apache.ibatis.plugin.Signature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.AnnotationUtils;
//import org.springframework.stereotype.Component;
//
//import java.lang.reflect.Field;
//import java.sql.PreparedStatement;
//import java.util.Objects;
//import java.util.Properties;
//
//@Component
//@Intercepts({
//    @Signature(type = ParameterHandler.class, method = "setParameters", args = PreparedStatement.class),
//})
//public class EncryptInterceptor implements Interceptor {
//
//  private EncryptService encryptService;
//
//  @Autowired
//  public EncryptInterceptor(EncryptService encryptService) {
//    this.encryptService = encryptService;
//  }
//
//  public Object intercept(Invocation invocation) throws Throwable {
//    //@Signature 指定了 type= parameterHandler 后，这里的 invocation.getTarget() 便是parameterHandler
//    //若指定ResultSetHandler ，这里则能强转为ResultSetHandler
//    ParameterHandler parameterHandler = (ParameterHandler) invocation.getTarget();
//    // 获取参数对像，即 mapper 中 paramsType 的实例
//    Field parameterField = parameterHandler.getClass().getDeclaredField("parameterObject");
//    parameterField.setAccessible(true);
//    //取出实例
//    Object parameterObject = parameterField.get(parameterHandler);
//    if (parameterObject != null) {
//      Class<?> parameterObjectClass = parameterObject.getClass();
//      //校验该实例的类是否被@SensitiveData所注解
//      SensitiveData sensitiveData = AnnotationUtils.findAnnotation(parameterObjectClass, SensitiveData.class);
//      if (Objects.nonNull(sensitiveData)) {
//        //取出当前当前类所有字段，传入加密方法
//        Field[] declaredFields = parameterObjectClass.getDeclaredFields();
//        encryptService.encrypt(declaredFields, parameterObject);
//      }
//    }
//    return invocation.proceed();
//  }
//
//  @Override
//  public Object plugin(Object target) {
//    return Interceptor.super.plugin(target);
//  }
//
//  @Override
//  public void setProperties(Properties properties) {
//    Interceptor.super.setProperties(properties);
//  }
//}